package cn.etg.day06;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;

/**
 * 使用字符流复制当前程序的源文件到当前项目根目录下。
 * @author Xiloer
 *
 */
public class Test05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入文件名: ");
        String fileName = scanner.nextLine();
        File file = new File(fileName);
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                (new FileOutputStream(file), "GBK"))) {
            while (true) {
                String input = scanner.nextLine();
                if ("exit".equalsIgnoreCase(input)) {
                        break;
                }
                writer.write(input);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scanner.close();
            System.out.println("内容已保存到文件: " + fileName);
        }
    }
}

